#include "win4_payment.h"

lv_obj_t *win4 = NULL;
lv_obj_t * win4_done = NULL;
lv_obj_t *win4_down_label;
lv_timer_t *timer1; // 定时器
int down_time = 10;
lv_timer_t *timer2;
static lv_style_t style; // 样式


static void return_cb1(lv_event_t* e) //按钮事件函数
{
    lv_event_code_t code = lv_event_get_code(e);  //同一个按钮发出不同不同事件
    if(code == LV_EVENT_CLICKED)   //判断按钮是什么事件发生
    {
       lv_obj_del(win4_done);
        ListDestory(&cart_head);
        Lis_commty_tDestory(&head);
        commodity_total=0;
        down_time = 10;
        //lv_timer_pause(timer2);
        lv_timer_del(timer2);
        win3_sell();//执行的内容
       
    }
}
static void my_timer2(lv_timer_t *timer)
{
    if (down_time== 0)
    {
        lv_obj_del(win4_done);
        ListDestory(&cart_head);
        Lis_commty_tDestory(&head);
        commodity_total=0;
        down_time = 10;
        //lv_timer_pause(timer2);
        printf("倒计时结束\n");
        lv_timer_del(timer2);
        printf("倒计时结束\n");
        win3_sell();
        
    }
    down_time--;
    printf("倒计时%d\n", down_time);
}

static void return_cb(lv_event_t *e) // 按钮事件函数
{
    lv_event_code_t code = lv_event_get_code(e); // 同一个按钮发出不同不同事件
    if (code == LV_EVENT_CLICKED)                // 判断按钮是什么事件发生
    {
        if (RDWR_Cancel() == 0)
        {   
            lv_style_reset(&style);
            lv_obj_del(win4);
            ListDestory(&cart_head);
            Lis_commty_tDestory(&head);
            commodity_total=0;
            
            win3_sell();
        }
        else
        {
            printf("删除失败\n");   //写一个消息框说明失败原因
        }
    }
}

static void pay_cb(lv_event_t *e) // 按钮事件函数
{
    lv_event_code_t code = lv_event_get_code(e); // 同一个按钮发出不同不同事件
    if (code == LV_EVENT_CLICKED)                // 判断按钮是什么事件发生
    {
        if(RDWR_int_order(commodity_total)==0)
        {
            //弄一个消息框，一个条件为成功，一个条件为失败，一个则对订单有异议
            printf("支付成功\n");
            lv_obj_del(win4);
            WIN4_done();
        }
    }
}


void WIN4_payment(void)
{
    list_oder();
    win4 = lv_obj_create(lv_scr_act());
    lv_obj_set_size(win4, 800, 480);
    lv_obj_set_style_pad_all(win4, 0, 0);
    lv_obj_clear_flag(win4, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_align(win4, LV_ALIGN_CENTER);


    lv_obj_t *order_img = lv_img_create(win4);
    LV_IMG_DECLARE(order);
    lv_img_set_src(order_img, &order);
    lv_obj_t *btn_return = lv_obj_create(win4);
    lv_obj_set_size(btn_return, 100, 50);
    lv_obj_set_align(btn_return, LV_ALIGN_BOTTOM_MID);
    lv_obj_clear_flag(btn_return, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_align(btn_return, LV_ALIGN_BOTTOM_MID);
    lv_obj_set_style_bg_color(btn_return, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_t *label = lv_label_create(btn_return);
    lv_label_set_text(label, "return");
    lv_obj_add_event_cb(btn_return, return_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *win4_weixi = lv_obj_create(win4);
    lv_obj_clear_flag(win4_weixi, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_align(win4_weixi, LV_ALIGN_CENTER);
    lv_obj_set_size(win4_weixi, 200, 287);
    lv_obj_align(win4_weixi, LV_ALIGN_CENTER,0,0);
    lv_obj_t *weixin_img = lv_img_create(win4_weixi);
    LV_IMG_DECLARE(weixin);
    lv_img_set_src(weixin_img, &weixin);
    lv_obj_align(weixin_img, LV_ALIGN_CENTER,0,0);
    lv_img_set_zoom(weixin_img, 200);
    lv_obj_add_event_cb(win4_weixi, pay_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *win4_zhifubao = lv_obj_create(win4);
    lv_obj_clear_flag(win4_zhifubao, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_align(win4_zhifubao, LV_ALIGN_CENTER);
    lv_obj_set_size(win4_zhifubao, 200, 287);
    lv_obj_align(win4_zhifubao, LV_ALIGN_CENTER,260,0);
    lv_obj_t *zhifubao_img = lv_img_create(win4_zhifubao);
    LV_IMG_DECLARE(zhifubao);
    lv_img_set_src(zhifubao_img, &zhifubao);
    lv_obj_align(zhifubao_img, LV_ALIGN_CENTER,0,0);
    lv_img_set_zoom(zhifubao_img, 170);
    lv_obj_add_event_cb(win4_zhifubao, pay_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *win4_user = lv_obj_create(win4);
    lv_obj_set_size(win4_user, 200, 300);
    lv_obj_align(win4_user, LV_ALIGN_CENTER,-260,0);

    lv_font_t *font = lv_freetype_font_create("/root/simkai.ttf", // 字体文件路径
                                              LV_FREETYPE_FONT_RENDER_MODE_BITMAP,
                                              40,
                                              LV_FREETYPE_FONT_STYLE_NORMAL);

    if (!font)
    {
        LV_LOG_ERROR("freetype font create failed.");
        return;
    }
    /*Create style with the new font*/
    lv_style_init(&style);
    lv_style_set_text_font(&style, font);
    lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);


    lv_obj_t *Tlabel_label = lv_label_create(win4);
    lv_obj_add_style(Tlabel_label, &style, 0);
    lv_label_set_text_fmt(Tlabel_label, "你需付总金额：￥%d元", commodity_total);
    lv_obj_align_to(Tlabel_label, win4_weixi,LV_ALIGN_OUT_TOP_MID,0,-10);
}

void WIN4_done(void)
{
    list_oder();
    win4_done = lv_obj_create(lv_scr_act());
    lv_obj_set_size(win4_done, 800, 480);
    lv_obj_set_style_pad_all(win4_done, 0, 0);
    lv_obj_clear_flag(win4_done, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_align(win4_done, LV_ALIGN_CENTER);

     lv_obj_t* img = lv_img_create(win4_done);
    LV_IMG_DECLARE(secc);
    lv_img_set_src(img, &secc);

    lv_obj_t *btn = lv_btn_create(win4_done);
    lv_obj_set_size(btn, 100, 50);
    lv_obj_set_align(btn, LV_ALIGN_TOP_RIGHT);
    win4_down_label = lv_label_create(btn);
    lv_obj_add_style(win4_down_label, &style, 0);
    lv_label_set_text_fmt( win4_down_label, "返回", down_time);
    lv_obj_align(win4_down_label, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(btn, return_cb1, LV_EVENT_CLICKED, NULL);

    timer2 = lv_timer_create(my_timer2, 1000, NULL);
}
void list_oder(void)
{  
    pLinkList p=cart_head->next;
    while(p!=NULL)
    {
        commodity_list_p q=head->next;
        while(q!=NULL)
        {
            if(strcmp(p->data,q->commodity->commodity_id)==0)
            {
                printf("商品名称：%s 商品数量：%d\n", q->commodity->commodity_name,p->set_number);
                break;
                sleep(1);
            }
            q=q->next;
        }
        p=p->next;
    }

}
